<?php
/**
 * @package PWE.modele
 * @author Gmax <gmaxfr78@gmail.com>
 */
class modele_region extends modele {
    protected static $_table = 'region';
    public $type = 'region';
    private $_id = 0;
    private $_nom = '';
    private $_departements = '';
    private $_category_id = 0;
    private $_category = null;
    
    function __construct($id=null){
        if($id!==null){
            $this->_id = $id;
            $this->_constructObject();
        }
    }
    private function _constructObject(){
        $sqlQuery = 'SELECT ';
        $sqlQuery .= 't.* ';
        $sqlQuery .= 'FROM '.self::getTable().' as t ';
        $sqlQuery .= 'WHERE t.id='.$this->_id.' ';
        $sqlQuery .= 'LIMIT 1';        
        
        $datas = $this->_getOneResult($sqlQuery);
        if($datas){
            $this->_id = $datas['id'];
            $this->_nom = $datas['nom'];
            $this->_departements =  $datas['departements'];
            $this->_category_id =  $datas['category_id'];
        }
    }
    public function getId(){
        return $this->_id;
    }
    public function getCategoryId(){
        return $this->_category_id;
    }
    
    /**
     * 
     * @return /modele_category|null
     */
    public function getCategory(){
        if(isset($this->_category)){
            return $this->_category;
        }elseif($this->getCategoryId()>0){
            $this->_category = new modele_category($this->getCategoryId());
            return $this->_category;
        }
        return null;
    }
    
    public function getTitle($input=false){
        if($this->getCategory()!=null){
            return $this->getCategory()->getTitle($input=false);
        }else{
            return $this->getNom($input);
        }
    }
    public function getDescriptionSEO(){
        return ($this->getCategory()!=null)?$this->getCategory()->getDescriptionSEO():'';
    }
    public function getTitreSEO(){
        return ($this->getCategory()!=null)?$this->getCategory()->getTitreSEO():'';
    }
    
    public function getNom($input=false){
        if($input)return $this->inputFormat(strtolower($this->_nom));
        else return ucwords($this->outputFormat($this->_nom));
    }
    public function getDepartements(){
        return $this->_departements;
    }
    public function getContent($input=false){
        return ($this->getCategory()!=null)?$this->getCategory()->getCustomVar('content'):'';
    }
    
    public function getDatas($input=false){
        $data = array();
        $data['id'] = $this->getId();
        $data['nom'] = $this->getNom($input);
        $data['category_id'] = $this->getCategoryId();
        $data['departements'] = $this->getDepartements();
        $data['liste_departements'] = explode('-',$this->getDepartements());
        $data['texte'] = $this->getTexte($input);
        return $data;
    }
    public function setDatas($datas) {
        foreach($datas as $k=>$v){
            if(substr($k, 0, 1) !== '_') $k = '_'.$k;
            if($k != '_id' && property_exists('modele_region',$k)){
                $this->$k = $v;
            }
        }
        return true;
    }
    public function setCategoryId($categoryId){
        if($categoryId>0){
            $this->_category_id = $categoryId;
            $sqlQueryAdresse = 'UPDATE '.self::getTable().' SET ';
            $sqlQueryAdresse .= 'category_id='.$this->getCategoryId().' ';
            $sqlQueryAdresse .= 'WHERE id='.$this->_id.' LIMIT 1';
            bdd_sql::doSql($sqlQueryAdresse,false);
            return array('success'=>true,'datas'=>$this->getDatas());
        }else{
            return array('success'=>false,'datas'=>$this->getDatas());
        }
    }
    public function update(){
        $sqlQueryAdresse = 'UPDATE '.self::getTable().' SET ';
        $sqlQueryAdresse .= 'nom="'.$this->getNom(true).'",';
        $sqlQueryAdresse .= 'category_id='.$this->getCategoryId().',';
        $sqlQueryAdresse .= 'departements="'.$this->getDepartements().'" ';
        $sqlQueryAdresse .= 'WHERE id='.$this->_id.' LIMIT 1';
        bdd_sql::doSql($sqlQueryAdresse,false);
        return array('success'=>true,'datas'=>$this->getDatas());
    }
    public function save(){
        $sqlQueryAdresse = 'INSERT INTO '.self::getTable().' (nom,category_id,departements) VALUES (';
        $sqlQueryAdresse .= '"'.$this->getNom(true).'",';
        $sqlQueryAdresse .= '"'.$this->getCategoryId().'",';
        $sqlQueryAdresse .= '"'.$this->getDepartements().'") ';
        bdd_sql::doSql($sqlQueryAdresse,false);
        $this->_id = bdd_sql::getLastInsertId();

        return array('success'=>true,'datas'=>$this->getDatas());
    }
    public function delete() {
        $sqlDelete = 'DELETE FROM '.self::getTable().' WHERE id='.$this->_id;
        bdd_sql::doSql($sqlDelete,false);

        return array('success'=>true,'datas'=>array());
    }
    
    /* function static */
    /**
     * 
     * @return string
     */
    public static function getTable(){
        return PWE::getPrefixBdd().self::$_table;
    }
    
    public static function getListeRegion(){
        $sqlRegion = 'SELECT * FROM region';
        $results = bdd_sql::doSql($sqlRegion,true);
        $retour = array();
        if($results['count']>0){
            foreach($results['results'] as $data ){
                $retour[] = new modele_region($data['id']);
            }
        }
        return $retour;
    }
    
    /**
     * retourne la region selon un code postal ou un num de departement
     * @param string $dept
     * @return \modele_region
     */
    public static function getRegionByDept($dept='00'){
        $retour = null;
        $dep = substr($dept,0,2);
        $sqlRegion = 'SELECT id FROM '.self::getTable().' WHERE ';
        $sqlRegion .= 'departements="'.$dep.'" OR ';
        $sqlRegion .= 'departements LIKE "'.$dep.'-%" OR ';
        $sqlRegion .= 'departements LIKE "%-'.$dep.'-%" OR ';
        $sqlRegion .= 'departements LIKE "%-'.$dep.'" LIMIT 1';
        $resultRegion = bdd_sql::doSql($sqlRegion,true);
        if($resultRegion['count']==1){
            $retour = new modele_region($resultRegion['results'][0]['id']);
        }
        return $retour;
    }
    
    public static function getRegionByCategory($category_id){
        if($category_id>0){
            $sqlQuery = 'SELECT id FROM '.self::getTable().' WHERE category_id='.$category_id.' LIMIT 1';
            $results = bdd_sql::doSql($sqlQuery,true);
            if($results['count']==1){
                return new modele_region($results['results'][0]['id']);
            }else{
                return null;
            }
        }else{
            return null;
        }
    }
}

?>